package com.fyzs.mp3recorder

import android.os.Bundle
import android.os.Handler
import android.os.HandlerThread
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import java.io.DataInputStream
import java.io.File
import java.io.FileInputStream
import java.nio.ShortBuffer
import java.util.*

class AnalyseFragment : Fragment() {
    val TAG = "AnalyseFragment"

    companion object {
        fun newInstance(path: String?): AnalyseFragment {
            val f = AnalyseFragment()
            f.arguments = Bundle().apply { putString("filePath", path) }
            return f
        }
    }


    val readThread = HandlerThread("read").apply { start() }
    val readHandler = Handler(readThread.looper)

    lateinit var lineChar: LineChart
    lateinit var waveView: PcmWaveView

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_analyse, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        lineChar = view.findViewById(R.id.line_char)
        waveView = view.findViewById(R.id.pcm_wave_view)
        getPcmFile()
    }


    fun getPcmFile() {
        readHandler.post {
            arguments?.getString("filePath")?.run {

                Log.i(TAG, "getPcmFile: $this")

                val pcmFile = File(this)
                waveView.setPcmFile(pcmFile)

            }
        }
    }


}